package hc.server.ui;

import hc.core.ContextManager;
import hc.core.MsgBuilder;
import hc.core.RootConfig;
import hc.core.RootServerConnector;
import hc.core.util.LogManager;
import hc.util.BackServer;
import hc.util.PropertiesManager;


public class ServerUIUtil {
	public static boolean useMainCanvas = PropertiesManager.isTrue(PropertiesManager.p_IsMobiMenu);
	public static BackServer responsor = initBackServer();
	
	public static BackServer initBackServer(){
		if(responsor != null){
			try{
				responsor.stop();
			}catch (Throwable e) {
				e.printStackTrace();
			}
		}
		
		if(useMainCanvas){
			//MobiUIResponsor
			String className = RootServerConnector.unObfuscate("chs.reev.riud.segi.noMibIUeRpsnoosr");
			try {
				responsor = (BackServer)Class.forName(className).newInstance();
			} catch (Exception e) {
				LogManager.err("load class:"+className);
				responsor = new DefaultUIResponsor();
			}
		}else{
			responsor = new DefaultUIResponsor();
		}
		try{
			responsor.start();
		}catch (Throwable e) {
			e.printStackTrace();
		}
		return responsor;
	}

	public static boolean response(String out) {
		ContextManager.getContextInstance().send(MsgBuilder.E_CANVAS_MAIN, out);
		return true;
	}

	public static String replaceIPWithHC(String ip){
		final String root = RootConfig.getInstance().getProperty(RootConfig.p_RootRelayServer);
		if(ip.indexOf(root) >= 0){
			return "homecenter.mobi[" + ip + "]";
		}else{
			return ip;
		}
	}

}
